#!/usr/bin/env bash

set -e

CURRENT_DIR=$(pwd)
source ${CURRENT_DIR}/log.sh "$(basename $0 .sh)-log-$(date "+%Y%m%d%H%M%S%1N")"

# 检查是否以 root 用户运行
if [ "$(id -u)" -ne 0 ]; then
    error "请使用 root 用户"
    exit 1
fi

###########################
start_info "开始配置 repo 源"
YUM_DIR_BAK="backup_$(date "+%Y%m%d%H%M%S%1N")"
REPO_FILE="CentOS-Base.repo"

# 备份原 repo 文件
info "开始备份 repo 文件..."
info "创建备份目录：/etc/yum.repos.d/${YUM_DIR_BAK}..."
mkdir "/etc/yum.repos.d/${YUM_DIR_BAK}"
pushd /etc/yum.repos.d &> /dev/null
info "备份原 repo 文件："
mv -v *.repo* "${YUM_DIR_BAK}"

# 配置华为云 yum 源
info "创建新的 repo 文件：${REPO_FILE}"
cat > "${REPO_FILE}" <<EOF
[BaseOS]
name=CentOS-\$releasever - Base - mirrors.huaweicloud.com
baseurl=https://mirrors.huaweicloud.com/centos-vault/8.5.2111/BaseOS/\$basearch/os/
gpgcheck=0

#released updates
[AppStream]
name=CentOS-\$releasever - AppStream - mirrors.huaweicloud.com
baseurl=https://mirrors.huaweicloud.com/centos-vault/8.5.2111/AppStream/\$basearch/os/
gpgcheck=0

[PowerTools]
name=CentOS-\$releasever - PowerTools - mirrors.huaweicloud.com
baseurl=https://mirrors.huaweicloud.com/centos-vault/8.5.2111/PowerTools/\$basearch/os/
gpgcheck=0

[extras]
name=CentOS-\$releasever - Extras - mirrors.huaweicloud.com
baseurl=https://mirrors.huaweicloud.com/centos-vault/8.5.2111/extras/\$basearch/os/
gpgcheck=0

[centosplus]
name=CentOS-\$releasever - Plus - mirrors.huaweicloud.com
baseurl=https://mirrors.huaweicloud.com/centos-vault/8.5.2111/centosplus/\$basearch/os/
gpgcheck=0
EOF

info "验证修改后的 repo 源是否可用："
yum clean all 
yum makecache
popd &> /dev/null

##########################
start_info "开始配置 pip 源"
PIP_CONFIG="/etc/pip.conf"
PIP_CONFIG_BAK="/etc/pip.conf.bak_$(date "+%Y%m%d%H%M%S%1N")"

# 备份原 pip 配置文件
if [ -f "${PIP_CONFIG}" ]; then
  info "备份原 pip 配置文件:"
  mv -v ${PIP_CONFIG} ${PIP_CONFIG_BAK}
fi

# 配置华为云 pip 源
info "创建 pip 配置文件：${PIP_CONFIG}"
cat > "${PIP_CONFIG}" <<EOF
[global]
index-url = https://mirrors.huaweicloud.com/repository/pypi/simple
trusted-host = mirrors.huaweicloud.com
timeout = 120
EOF

#############################
start_info "开始修改 sshd 配置"
SSHD_CONFIG="/etc/ssh/sshd_config"
SSHD_CONFIG_BAK="/etc/ssh/sshd_config.bak_$(date "+%Y%m%d%H%M%S%1N")"

# 定义要修改的参数
declare -A PARAMS=(
    ["ClientAliveInterval"]="60"
    ["ClientAliveCountMax"]="30"
    ["TCPKeepAlive"]="yes"
)

# 备份 /etc/ssh/sshd_config 文件
info "创建备份文件: ${SSHD_CONFIG_BAK}"
cp "${SSHD_CONFIG}" "${SSHD_CONFIG_BAK}"

# 修改配置文件
info "开始修改配置文件：${SSHD_CONFIG}"
for param in "${!PARAMS[@]}"; do
    value="${PARAMS[${param}]}"
    info "处理参数: ${param}"
    # 检查参数是否已存在(包括注释和非注释状态)
    if grep -q -E "^#?\s*${param}\s+" "${SSHD_CONFIG}"; then
        # 如果存在(已注释或未注释)，则替换为新值
        sed -i -r "s/^#?\s*${param}\s+.*/${param} ${value}/" "${SSHD_CONFIG}"
    else
        # 如果不存在(新参数)，添加到文件末尾
        info "${param} ${value}" >> "${SSHD_CONFIG}"
    fi
done

info "配置已完成，以下是变更摘要:"
for param in "${!PARAMS[@]}"; do
    info "  ${param} = ${PARAMS[${param}]}"
    grep -E "^${param}\s+" "${SSHD_CONFIG}"
done

info "重启 SSH 服务"
systemctl restart sshd